﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Derrick.AttackTest
{
    public partial class MainForm : Form
    {

        private static Thread _attackThread = null;
        private static SpoofService _spoofingService = null;


        public MainForm()
        {
            InitializeComponent();
            _debugInfoBindingSource.DataSource = SharedStorage.Instance.DebugInfo;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _attackThread = new Thread(new ThreadStart(Spoofing));
            _attackThread.Start();
        }

        public void Spoofing()
        {
            _spoofingService = new SpoofService();
            _spoofingService.SpoofEvent += new EventHandler<SpoofEventArgs>(_spoofingService_SpoofEvent);
            _spoofingService.StartSpoofing();
        }

        void _spoofingService_SpoofEvent(object sender, SpoofEventArgs args)
        {
            //throw new NotImplementedException();
            SharedStorage.Instance.AddDebugInfo("Spoof", args.Message, SharedStorage.Instance.PhonyIpAddress, SharedStorage.Instance.DestinationIpAddress);
            _debugInfoBindingSource.DataSource = SharedStorage.Instance.DebugInfo;
            //_outputGrid.Refresh();
        }

        private void configureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetSpoofParameters setParamsWnd = new SetSpoofParameters();
            setParamsWnd.Show();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_attackThread != null)
            {
                _spoofingService.StopSpoofing();
                _attackThread.Abort();
            }
        }

        private void _uiTimer_Tick(object sender, EventArgs e)
        {
            _debugInfoBindingSource.DataSource = SharedStorage.Instance.DebugInfo;
            _outputGrid.Refresh();
        }

        private void hTTPTracingToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
